package pl.edu.pk.mech.kkbus.controller;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import pl.edu.pk.mech.kkbus.model.New;
import pl.edu.pk.mech.kkbus.model.ResponseStatus;
import pl.edu.pk.mech.kkbus.service.NewService;

@Controller
public class NewController extends RestController {
    
    @Autowired
    NewService newService;
	
    private static final Logger log = Logger.getLogger(NewController.class.getName());

    @RequestMapping(value = "/news", method = RequestMethod.GET)
    public @ResponseBody List<New> getNews(@RequestParam(required = false, defaultValue = "-1") int page) {
        if (page < 0) {
            log.info("Get all news");
            return newService.getAllNews();
        } else {        
            log.info("Get part news. Page: " + page);
            return null;
        }         
    }
    
    @RequestMapping(value = "/news", method = RequestMethod.POST)
    public @ResponseBody ResponseStatus createNews(@RequestBody New news) {
    	newService.addNews(news);
        return ResponseStatus.ADDED;
    }
    
    @RequestMapping(value = "/news/{id}", method = RequestMethod.PUT)
    public @ResponseBody ResponseStatus updateNews(@PathVariable String id, @RequestBody New news) {
    	// TODO Call service method
        boolean result = false;
        return result ? ResponseStatus.UPDATED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/news/{id}", method = RequestMethod.DELETE)
    public @ResponseBody ResponseStatus deleteNews(@PathVariable String id) {
    	// TODO Call service method
        boolean result = false;
        return result ? ResponseStatus.DELETED : ResponseStatus.ERROR;
    }
}
